#ifndef __nRF2401_H_
#define __nFR2401_H_		

/*-------------------------------------------------------------------------
 * MPLAB-Cxx  PIC24FJ16GA002 processor header
 *
 * (c) Copyright 1999-2008 Microchip Technology, All rights reserved
 *-------------------------------------------------------------------------*/
#include "p24FJ16GA002.h"
//�˿�����˵��====================================================
//  PWR 
//  CK1
//  CE  
//  CS  
//  DAT 
//================================================================
//TODO finish the port mapping
#if defined(__PIC24FJ16GA002__)
/*#define SET_PWR()   LATBbits.LATB15=1
#define SET_CE()    LATBbits.LATB13=1
#define SET_CS()    LATBbits.LATB12=1
#define SET_CLK()   LATBbits.LATB10=1
#define SET_DATA()  LATBbits.LATB9=1
#define SET_DR()    LATBbits.LATB11=1

#define GET_DATA()    PORTBbits.RB9
#define GET_DR()      PORTBbits.RB11

#define CLEAR_PWR()   LATBbits.LATB15=0
#define CLEAR_CE()    LATBbits.LATB13=0
#define CLEAR_CS()    LATBbits.LATB12=0
#define CLEAR_CLK()   LATBbits.LATB10=0
#define CLEAR_DATA()  LATBbits.LATB9=0
#define CLEAR_DR()    LATBbits.LATB11=0


#define SET_LED()		LATBbits.LATB14=1
#define CLEAR_LED()   LATBbits.LATB14=0
 */

#define SET_PWR()   LATBbits.LATB15=1
#define SET_CE()    LATBbits.LATB13=1
#define SET_CS()    LATBbits.LATB12=1
#define SET_CLK()   LATBbits.LATB10=1
#define SET_DATA()  LATBbits.LATB9=1
#define SET_DR()    LATBbits.LATB11=1

#define GET_DATA()    PORTBbits.RB9
#define GET_DR()      PORTBbits.RB11

#define CLEAR_PWR()   LATBbits.LATB15=0
#define CLEAR_CE()    LATBbits.LATB13=0
#define CLEAR_CS()    LATBbits.LATB12=0
#define CLEAR_CLK()   LATBbits.LATB10=0
#define CLEAR_DATA()  LATBbits.LATB9=0
#define CLEAR_DR()    LATBbits.LATB11=0


#define SET_LED()		LATBbits.LATB14=1
#define CLEAR_LED()   LATBbits.LATB14=0


#elif defined(__PIC18F97J60__)
#define SET_PWR()   PORTGbits.RG6=1
#define SET_CE()    PORTGbits.RG5=1
#define SET_CS()    PORTFbits.RF0=1
#define SET_CLK()   PORTGbits.RG4=1
#define SET_DATA()  PORTFbits.RF7=1
#define SET_DR()    PORTBbits.RB0=1

#define GET_DATA()    PORTFbits.RF7
#define GET_DR()      PORTBbits.RB0

#define CLEAR_PWR()   PORTGbits.RG6=0
#define CLEAR_CE()    PORTGbits.RG5=0
#define CLEAR_CS()    PORTFbits.RF0=0
#define CLEAR_CLK()   PORTGbits.RG4=0
#define CLEAR_DATA()  PORTFbits.RF7=0
#define CLEAR_DR()    PORTBbits.RB0=0

#define SET_PWR_2()   PORTFbits.RF5=1
#define SET_CE_2()    PORTFbits.RF4=1
#define SET_CS_2()    PORTFbits.RF3=1
#define SET_CLK_2()   PORTFbits.RF2=1
#define SET_DATA_2()  PORTHbits.RH7=1
#define SET_DR_2()    PORTBbits.RB1=1

#define GET_DATA_2()  PORTHbits.RH7
#define GET_DR_2()    PORTBbits.RB1

#define CLEAR_PWR_2()   PORTFbits.RF5=0
#define CLEAR_CE_2()    PORTFbits.RF4=0
#define CLEAR_CS_2()    PORTFbits.RF3=0
#define CLEAR_CLK_2()   PORTFbits.RF2=0
#define CLEAR_DATA_2()  PORTHbits.RH7=0
#define CLEAR_DR_2()    PORTBbits.RB1=0

#endif 

#define TEST_2		0x8E			//MSB	D143~D136
#define TEST_1		0x08			//		D135~D128
#define TEST_0		0x1C			//		D127~D120

/* ע��: DATAx_W + ADDRx_W + CRC ��ֵ����С��256 !  ������ݰ�Ĵ�С����С��32�ֽ�(256λ) */
#define DATA2_W		0x10	//2�ֽ�	    //Ƶ��2 ��ݳ���?��λ:Bit)
#define DATA1_W		0xa0	//20�ֽ�		//Ƶ��1 ��ݳ���?��λ:Bit)

//					0xE0 = 224
//16bit Address + 224bit(28byte)Data + 16bit CRC = 256bit

/* ע��:2401����ADDR�г���ADDR_W�趨��ȵ���Щ�?ͬʱ��ַ����ȫ������Ϊ0 */
//Ƶ��2 ���յ�ַ(��ǰģ����?
#define ADDR2_4		0x00
#define ADDR2_3		0x1c
#define ADDR2_2		0xcc
#define ADDR2_1		0xcc								
#define ADDR2_0		0xcc
//Ƶ��1 ���յ�ַ
#define ADDR1_4		0xcc
#define ADDR1_3		0xcc
#define ADDR1_2		0xcc
#define ADDR1_1		0xcc
#define ADDR1_0		0xcc

#define ADDR_W		0x20					//4�ֽ�	//���յ�ַ���?��λ:Bit)
#define CRC_L			0x1					//CRCģʽ 0:8λ	1:16
#define CRC_EN		0x1					//CRCУ������

#define RX2_EN		0x0					//˫Ƶ�?�������?
#define CM			0x1					//0:Direct mode	1:ShockBurst mode
#define RFDR_SB		0x1					//0:250kbps		1:1Mbps
#define XO_F			0x3					//16M		//nRF2401����Ƶ��
#define RF_PWR		0x3					//�źŷ��书��

#define RF_CH			0x2					//Channel RF Ƶ��
#define RXEN			0x0

#define ADDR1_4_2		0xcc
#define ADDR1_3_2		0xcc
#define ADDR1_2_2		0xcc
#define ADDR1_1_2		0xcc
#define ADDR1_0_2		0xcc

#define RF_CH_2		0x2



//������������ô������

//<��������Ϣ��ϳ�ÿ���ֽڵ������Ϣ
#define	RFConfig_Byte0	TEST_2
#define	RFConfig_Byte1	TEST_1
#define	RFConfig_Byte2	TEST_0
#define	RFConfig_Byte3	DATA2_W
#define	RFConfig_Byte4	DATA1_W
#define	RFConfig_Byte5	ADDR2_4
#define	RFConfig_Byte6	ADDR2_3
#define	RFConfig_Byte7	ADDR2_2
#define	RFConfig_Byte8	ADDR2_1
#define	RFConfig_Byte9	ADDR2_0
#define	RFConfig_Byte10	ADDR1_4
#define	RFConfig_Byte11	ADDR1_3
#define	RFConfig_Byte12	ADDR1_2
#define	RFConfig_Byte13	ADDR1_1
#define	RFConfig_Byte14	ADDR1_0
#define	RFConfig_Byte15	(ADDR_W<<2|CRC_L<<1|CRC_EN)
#define	RFConfig_Byte16	(RX2_EN<<7|CM<<6|RFDR_SB<<5|XO_F<<2|RF_PWR)
#define	RFConfig_Byte17	(RF_CH<<1|RXEN)

#define	RFConfig_Byte10_2	ADDR1_4_2
#define	RFConfig_Byte11_2	ADDR1_3_2
#define	RFConfig_Byte12_2	ADDR1_2_2
#define	RFConfig_Byte13_2	ADDR1_1_2
#define	RFConfig_Byte14_2	ADDR1_0_2
#define	RFConfig_Byte16_2	(RX2_EN<<7|CM<<6|RFDR_SB<<5|XO_F<<2|RF_PWR)
#define	RFConfig_Byte17_2	(RF_CH_2<<1|RXEN)


//�˿�����˵��====================================================
//  PWR 
//  CK1
//  CE  
//  CS  
//  DAT 
//================================================================

#define SET_PWR()   LATBbits.LATB15=1
#define SET_CE()    LATBbits.LATB13=1
#define SET_CS()    LATBbits.LATB12=1
#define SET_CLK()   LATBbits.LATB10=1
#define SET_DATA()  LATBbits.LATB9=1
#define SET_DR()    LATBbits.LATB11=1

#define GET_DATA()    PORTBbits.RB9
#define GET_DR()      PORTBbits.RB11

#define CLEAR_PWR()   LATBbits.LATB15=0
#define CLEAR_CE()    LATBbits.LATB13=0
#define CLEAR_CS()    LATBbits.LATB12=0
#define CLEAR_CLK()   LATBbits.LATB10=0
#define CLEAR_DATA()  LATBbits.LATB9=0
#define CLEAR_DR()    LATBbits.LATB11=0


#define SET_LED()     LATBbits.LATB14=1
#define CLEAR_LED()   LATBbits.LATB14=0



#define TEST_2		0x8E			//MSB	D143~D136
#define TEST_1		0x08			//		D135~D128
#define TEST_0		0x1C			//		D127~D120

/* ע��: DATAx_W + ADDRx_W + CRC ��ֵ����С��256 !  ������ݰ�Ĵ�С����С��32�ֽ�(256λ) */
#define DATA2_W		0x10	//2�ֽ�	    //Ƶ��2 ��ݳ���(��λ:Bit)
#define DATA1_W		0xa0	//20�ֽ�		//Ƶ��1 ��ݳ���(��λ:Bit)


//16bit Address + 224bit(28byte)Data + 16bit CRC = 256bit

/* ע��:2401����ADDR�г���ADDR_W�趨��ȵ���Щλ,ͬʱ��ַ����ȫ������Ϊ0 */
//Ƶ��2 ���յ�ַ(��ǰģ���ַ)
#define ADDR2_4		0xcc
#define ADDR2_3		0xcc
#define ADDR2_2		0xcc
#define ADDR2_1		0xcc								
#define ADDR2_0		0xcc
//Ƶ��1 ���յ�ַ
#define ADDR1_4		0xcc
#define ADDR1_3		0xcc
#define ADDR1_2		0xcc
#define ADDR1_1		0xcc
#define ADDR1_0		0xcc

#define ADDR_W		0x20					//4�ֽ�	//���յ�ַ���(��λ:Bit)
#define CRC_L			0x1					//CRCģʽ 0:8λ	1:16
#define CRC_EN		0x1					//CRCУ������

#define RX2_EN		0x0					//˫Ƶ�9�������
#define CM			0x1					//0:Direct mode	1:ShockBurst mode
#define RFDR_SB		0x1					//0:250kbps		1:1Mbps
#define XO_F			0x3					//16M		//nRF2401����Ƶ��
#define RF_PWR		0x3					//�źŷ��书��

#define RF_CH			0x2					//Channel RF Ƶ��
#define RXEN			0x0







//������������ô������

//<��������Ϣ��ϳ�ÿ���ֽڵ������Ϣ
#define	RFConfig_Byte0	TEST_2
#define	RFConfig_Byte1	TEST_1
#define	RFConfig_Byte2	TEST_0
#define	RFConfig_Byte3	DATA2_W
#define	RFConfig_Byte4	DATA1_W
#define	RFConfig_Byte5	ADDR2_4
#define	RFConfig_Byte6	ADDR2_3
#define	RFConfig_Byte7	ADDR2_2
#define	RFConfig_Byte8	ADDR2_1
#define	RFConfig_Byte9	ADDR2_0
#define	RFConfig_Byte10	ADDR1_4
#define	RFConfig_Byte11	ADDR1_3
#define	RFConfig_Byte12	ADDR1_2
#define	RFConfig_Byte13	ADDR1_1
#define	RFConfig_Byte14	ADDR1_0
#define	RFConfig_Byte15	(ADDR_W<<2|CRC_L<<1|CRC_EN)
#define	RFConfig_Byte16	(RX2_EN<<7|CM<<6|RFDR_SB<<5|XO_F<<2|RF_PWR)
#define	RFConfig_Byte17	(RF_CH<<1|RXEN)





//---------------------------λ������----------------------------------------

#define BYTE_BIT0	0x01
#define BYTE_BIT1	0x02
#define BYTE_BIT2	0x04
#define BYTE_BIT3	0x08
#define BYTE_BIT4	0x10
#define BYTE_BIT5	0x20
#define BYTE_BIT6	0x40
#define BYTE_BIT7	0x80

#define DATA7   ((DATA_BUF&BYTE_BIT7)!=0)
#define DATA0	((DATA_BUF&BYTE_BIT0)!=0)



unsigned char nRF2401_Conf[18] = {
    RFConfig_Byte0,
    RFConfig_Byte1,
    RFConfig_Byte2,
    RFConfig_Byte3,
    RFConfig_Byte4,
    RFConfig_Byte5,
    RFConfig_Byte6,
    RFConfig_Byte7,
    RFConfig_Byte8,
    RFConfig_Byte9,
    RFConfig_Byte10,
    RFConfig_Byte11,
    RFConfig_Byte12,
    RFConfig_Byte13,
    RFConfig_Byte14,
    RFConfig_Byte15,
    RFConfig_Byte16,
    RFConfig_Byte17
};

//function declare
//see nRF2401.c for detail info
//void nrf2401_port_init(void);
//void set_tx_addr(unsigned long int addr);
//void set_nrf_channel(unsigned char i);
//void RF2401_TxPacket(unsigned char TxBuf[]);
//unsigned char RF2401_RxPacket(unsigned char *RxBuf);
#endif //__nRF2401_H_
